package com.xiang.activity;

import java.util.ArrayList;
import java.util.List;

import com.xiang.adapter.CityAdapter;
import com.xiang.entity.GetCitiesEntity;
import com.xiang.seemovies.R;
import com.xiang.utils.ActivityJumpUril;
import com.xiang.utils.JsonUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cn.edu.fzxy.zxy.niceday.AsyncHttpGet;
import cn.edu.fzxy.zxy.niceday.DefaultThreadPool;
import cn.edu.fzxy.zxy.niceday.RequestResultCallback;
import cn.edu.fzxy.zxy.niceday.examples.BaseActivity;

/**
 * 获取支持团购搜索的最新城市列表
 * 
 * @author Administrator
 * 
 */
public class GetCitiesActivity extends BaseActivity {
	List<String> cityList = new ArrayList<String>();// 城市列表
	private ListView listview;
	private CityAdapter adapter;
	private int SHOW_CITY_LIST = 10000;
	private MyHandler handler;
	private ImageView back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.city_layout);
		initView();
		request();
		if (handler == null) {
			handler = new MyHandler();
		}
	}

	private void initView() {
		listview = (ListView) findViewById(R.id.listview_city_list);
		listview.setOnItemClickListener(new ItemClickListener());
		back = (ImageView) findViewById(R.id.get_cities_back);
		back.setOnClickListener(new ImageView.OnClickListener() {
			@Override
			public void onClick(View v) {
				GetCitiesActivity.this.finish();
				overridePendingTransition(R.anim.left_in, R.anim.right_out);
			}
		});
	}

	class ItemClickListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			ActivityJumpUril.citiesActivityToRegionsActivity(
					GetCitiesActivity.this, cityList.get(arg2));
			overridePendingTransition(R.anim.right_in, R.anim.left_out);
		}

	}

	private void request() {
		GetCitiesEntity entity = new GetCitiesEntity();
		AsyncHttpGet get = new AsyncHttpGet(null, entity.getUrl(),
				entity.getParameterList(), new RequestResultCallback() {

					@Override
					public void onSuccess(Object o) {
						System.out.println("#########result: " + o.toString());
						cityList = JsonUtil.analysisCities(o.toString());
						Message msg = new Message();
						msg.what = SHOW_CITY_LIST;
						handler.sendMessage(msg);
					}

					@Override
					public void onFail(Exception e) {
						System.out.println("#########Exception");

					}
				});

		DefaultThreadPool.getInstance().execute(get);
		this.requestList.add(get);
	}

	private void showList() {
		if (adapter == null) {
			adapter = new CityAdapter(GetCitiesActivity.this, cityList);
			listview.setAdapter(adapter);
		} else {
			adapter.resetData(cityList);
			adapter.notifyDataSetChanged();
		}
	}

	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == SHOW_CITY_LIST) {
				showList();
			}
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                 && event.getRepeatCount() == 0) {
        	GetCitiesActivity.this.finish();
        	overridePendingTransition(R.anim.left_in, R.anim.right_out);
             return true;
         }
         return super.onKeyDown(keyCode, event);
     }
}
